package com.example.password.db.database;

import android.content.Context;

import androidx.room.Database;
import androidx.room.Room;
import androidx.room.RoomDatabase;

import com.example.password.db.dao.PasswordDao;
import com.example.password.db.entity.Password;

/**
 * 密码表数据库配置
 */
@Database(entities = {Password.class}, version = 1)
public abstract class PasswordDatabase extends RoomDatabase {
    public abstract PasswordDao passwordDao();
    /* 单例模式,确保全局只有一个数据库实例 */
    private static volatile PasswordDatabase INSTANCE;

    public static PasswordDatabase getInstance(Context context) {
        if (INSTANCE == null){
            synchronized (PasswordDatabase.class) {
                if (INSTANCE == null) {
                    INSTANCE = Room.databaseBuilder(context.getApplicationContext(),PasswordDatabase.class,"password_database.db")
                            .allowMainThreadQueries().build();
                }
            }
        }
        return INSTANCE;
    }
}
